package com.zyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zyz.dao.CourseDao;
import com.zyz.dao.UserLessonLineDao;
import com.zyz.domain.Lesson;
import com.zyz.domain.User;
import com.zyz.domain.UserLessonLine;
import com.zyz.handler.UserHolder;
import com.zyz.service.CourseService;
import com.zyz.service.UserLessonLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private UserLessonLineDao userLessonLineDao;
    //分页查询课程
    @Override
    public PageInfo<Lesson> getPage(Integer pageNum, Integer pageSize) {
        User user = UserHolder.get();
        Integer userId = user.getId();
        PageHelper.startPage(pageNum, pageSize);
        List<Lesson> list = courseDao.selectList(null);
        for (Lesson lesson : list) {
            //课程id
            Integer id = lesson.getId();
            QueryWrapper<UserLessonLine> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(UserLessonLine::getLesson_id,id);
            wrapper.lambda().eq(UserLessonLine::getMember_id,userId);
            UserLessonLine one = userLessonLineDao.selectOne(wrapper);
            if (one!=null){
                lesson.setChoose("已选择");
            }else {
                lesson.setChoose("未选择");
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public void addLessonToUser(Integer id) {
        Integer userId = UserHolder.get().getId();
        UserLessonLine userLessonLine = new UserLessonLine();
        userLessonLine.setLesson_id(id);
        userLessonLine.setMember_id(userId);
        userLessonLineDao.insert(userLessonLine);

    }

    @Override
    public String haveLesson() {
        Integer id = UserHolder.get().getId();
        QueryWrapper<UserLessonLine> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserLessonLine::getMember_id,id);
        UserLessonLine userLessonLine = userLessonLineDao.selectOne(wrapper);
        String msg=null;
        if (userLessonLine==null){
            msg="no";
        }else {
            msg="have";
        }
        return  msg;
    }

    @Override
    public Integer getLessonTotal() {
        List<Lesson> lessonList = courseDao.selectList(null);
        int i=0;
        for (Lesson lesson : lessonList) {
            i++;
        }
        return i;
    }
}
